﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;

using BlueCMS.Core.Entities;
using BlueCMS.Core.Services;

namespace BlueCMS.Core.Pages
{
    public class PageHandler : Page
    {
        public PageHandler()
        {
            this.PreInit += new EventHandler(PageHandler_PreInit);
        }

        void PageHandler_PreInit(object sender, EventArgs e)
        {
            // Load the page for this node
            Node node = CmsApplication.CmsNode;
            if(node == null)
            {
                throw new HttpException("Page handler called with no node");
            }

            Webpage page = WebpageService.GetWebpageForNode(node);
            if (page == null)
            {
                throw new HttpException("No page found mapped to this node");
            }

            // If we can edit the page then show the toolbar
            if (UserService.UserHasPermissionForNodeOrChild(CmsApplication.CmsUser, "NODE", "EDIT", node))
            {
                Control form = FindControl("form");
                if (form != null)
                {
                    Control control = Page.LoadControl("~/_System/Controls/WebpageToolbar.ascx");
                    form.Controls.AddAt(0, control);
                }
            }


            // Load the template for this page
            Control contentArea = FindControl("content_area");
            if (contentArea != null)
            {
                foreach (TemplateFragment fragment in page.Template.Fragments)
                {
                    switch (fragment.type_cd)
                    {
                        case "LITERAL":
                            {
                                contentArea.Controls.Add(new LiteralControl(fragment.literal));
                            }
                            break;
                        case "CONTENT_REGION":
                            {
                                ContentRegion region = new ContentRegion(fragment.region_nm, fragment.ismasterregion, page);
                                contentArea.Controls.Add(region);
                            }
                            break;
                    }
                }
            }

            // Add in the header attributes
            HtmlHead head = FindControl("head") as HtmlHead;
            HtmlGenericControl meta;

            head.Title = page.title;

            meta = new HtmlGenericControl("meta");
            meta.Attributes.Add("name", "keywords");
            meta.Attributes.Add("value", page.keywords);
            head.Controls.Add(meta);

            meta = new HtmlGenericControl("meta");
            meta.Attributes.Add("name", "desscription");
            meta.Attributes.Add("value", page.description);
            head.Controls.Add(meta);

            // Add default system css and js files
            foreach (string file in Directory.GetFiles(Server.MapPath("~/_System/css")))
            {
                HtmlGenericControl cssLink = new HtmlGenericControl("link");
                cssLink.Attributes.Add("href", Page.ResolveClientUrl("~/_System/css/" + Path.GetFileName(file)));
                cssLink.Attributes.Add("rel", "stylesheet");
                cssLink.Attributes.Add("type", "text/css");
                head.Controls.Add(cssLink);
            }

            foreach (string file in Directory.GetFiles(Server.MapPath("~/_System/js")))
            {
                ClientScript.RegisterClientScriptInclude(Path.GetFileName(file), Page.ResolveClientUrl("~/_System/js/" + Path.GetFileName(file)));
            }
        }
    }
}
